﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Reflection;
using System.IO;
using System.Drawing;
using MyUtils;
namespace Resources
{
    public class ResourceHelper
    {
        
        private static String currentResourcePath = String.Empty; 
        /// <summary>
        /// Save language to file
        /// </summary>
        /// <param name="currentLanguage"></param>
        /// 
        #region Saving language
        public static void SaveLanguague(LANGUAGE currentLanguage)
        {
            //Setcurrent Path Lang;
            switch (currentLanguage)
            {
                case LANGUAGE.ENGLISH:
                    currentResourcePath = RESOURCE_PATH.ENGLISH;
                    break;
                case LANGUAGE.VIETNAMESE:
                    currentResourcePath = RESOURCE_PATH.VIETNAMESE;
                    break;
            }
            FileUtils.CheckFileExist(RESOURCE_PATH.CONFIG_FOLDER);
            FileStream fileStream = new FileStream(FileUtils.GetExePath() + RESOURCE_PATH.CONFIG_FILE_LANGUAGE, FileMode.OpenOrCreate);
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(currentLanguage.ToString());
            //delete content
            fileStream.SetLength(0);
            fileStream.Flush();
            fileStream.Write(buffer, 0, buffer.Length);
            fileStream.Close();
        }
        #endregion 
        /// <summary>
        /// Read file and get saving language
        /// </summary>
        /// <returns></returns>
        /// 
        #region GetCurrentLanguage
        public static LANGUAGE GetCurrentLanguage()
        {
            //if not exist folder, create newfolder
            FileUtils.CheckFileExist(RESOURCE_PATH.CONFIG_FOLDER);
            //if file doesn't exist create new config language file
            FileStream fileStream = new FileStream(FileUtils.GetExePath() + RESOURCE_PATH.CONFIG_FILE_LANGUAGE, FileMode.OpenOrCreate);
            byte[] buffer;
            LANGUAGE langue = LANGUAGE.ENGLISH;//default
            currentResourcePath = RESOURCE_PATH.ENGLISH;
            try
            {
                // read from file or write to file
                int length = (int)fileStream.Length;  // get file length
                buffer = new byte[length];            // create buffer
                int count;                            // actual number of bytes read
                int sum = 0;                          // total number of bytes read

                // read until Read method returns 0 (end of the stream has been reached)
                while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                    sum += count;  // sum is a buffer offset for next reading
                string s = System.Text.Encoding.ASCII.GetString(buffer);
                LANGUAGE temp=LANGUAGE.ENGLISH;
                if (Enum.IsDefined(typeof(LANGUAGE), s))
                {
                    temp = (LANGUAGE)Enum.Parse(typeof(LANGUAGE), s, true);
                  
                }
                else
                {
                    //Write file because have right data
                    //Write to file
                    byte[] writeBytes = System.Text.Encoding.UTF8.GetBytes(LANGUAGE.ENGLISH.ToString());
                    //if content is false , delete all
                    fileStream.SetLength(0);
                    fileStream.Flush();
                    fileStream.Write(writeBytes, 0, writeBytes.Length); 

                }
                switch (temp)
                {
                    case LANGUAGE.ENGLISH:
                        currentResourcePath = RESOURCE_PATH.ENGLISH;
                        break;
                    case LANGUAGE.VIETNAMESE:
                        currentResourcePath = RESOURCE_PATH.VIETNAMESE;
                        break;
                }  
                    langue = temp; 

            }
 
            finally
            {
                fileStream.Close();
            }

            return langue;
        }
        #endregion 
        /// <summary>
        /// Load String
        /// </summary>
        /// <param name="ResourceID"></param>
        /// <returns></returns>
        #region Load string from resoure
        public static String LoadString(String resourceID)
        {
            ResourceManager rm = new ResourceManager(currentResourcePath, Assembly.GetExecutingAssembly());
            String result = rm.GetString(resourceID);
            return result;
        }
        #endregion
        /// <summary>
        /// Load bitmap resource
        /// </summary>
        /// <param name="ResourceID"></param>
        /// <returns></returns>
        #region Load bitmap resource
        public static Bitmap LoadBitmap(String resourceID)
        {
            ResourceManager rm = new ResourceManager(RESOURCE_PATH.BITMAP_RESOURCES, Assembly.GetAssembly(typeof(Reference)));
            Bitmap bitmap = (Bitmap)rm.GetObject(resourceID);
            return bitmap;
        }
        public static Icon LoadIcon(String resourceID)
        {
            ResourceManager rm = new ResourceManager(RESOURCE_PATH.BITMAP_RESOURCES, Assembly.GetAssembly(typeof(Reference)));
            Bitmap bitmap = (Bitmap)rm.GetObject(resourceID);
            System.IntPtr icH =  bitmap.GetHicon();
             Icon icon = Icon.FromHandle(icH);
            return icon;
        }
        public static Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
        {
            Bitmap result = new Bitmap(nWidth, nHeight);
            using (Graphics g = Graphics.FromImage((Image)result))
                g.DrawImage(b, 0, 0, nWidth, nHeight);
            return result;
        }
        public static Bitmap Get_Convert_BitMap(String resourceID)
        {
            Bitmap bm = LoadBitmap(resourceID);
            bm = ResizeBitmap(bm, MYMESSAGEBOX_NUMBER.INT_WIDTH_BITMAP, MYMESSAGEBOX_NUMBER.INT_HEIGHT_BITMAP);
            return bm;
        }
        #endregion 
    }
}
